package com.jtny.nytb.equipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jtny.component.util.BeanUtils;
import com.jtny.nytb.equipment.dao.equipment.UserEquipmentRelationDAO;
import com.jtny.nytb.equipment.domain.equipment.UserEquipmentRelationDO;
import com.jtny.nytb.equipment.dto.device.EquipmentInfoDTO;
import com.jtny.nytb.equipment.dto.device.EquipmentInfoQueryPageParamDTO;
import com.jtny.nytb.equipment.service.EquipmentInfoService;
import com.jtny.nytb.equipment.service.UserEquipmentRelationService;
import com.jtny.nytb.equipment.vo.device.EquipmentInfoVO;
import com.jtny.nytb.equipment.vo.device.UserEquipmentQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 智慧能源中心 - 车智斌
 * @package com.jtny.energymonitor.equipment.service.impl
 * @fileName UserEquipmentRelationServiceImpl.java
 * @createTime 2024年03月19日 10:38
 * @Copyright© 2024 jtny
 * 江投能源技术研究院 版权所有
 */
@Service
public class UserEquipmentRelationServiceImpl implements UserEquipmentRelationService {

    @Autowired
    private UserEquipmentRelationDAO userEquipmentRelationDAO;
    @Autowired
    private EquipmentInfoService equipmentInfoService;

    @Override
    public List<UserEquipmentQueryVO> queryUserEquipmentRelationByCodes(List<String> userCodelist) {

        List<UserEquipmentQueryVO> list = new ArrayList<>();

        if (!userCodelist.isEmpty()){

            QueryWrapper<UserEquipmentRelationDO> queryWrapper = new QueryWrapper<>();

            queryWrapper.in("user_code",userCodelist);

            List<UserEquipmentRelationDO> userEquipmentRelationDOS = userEquipmentRelationDAO.selectList(queryWrapper);

            Map<String, List<UserEquipmentRelationDO>> collect = userEquipmentRelationDOS.stream().collect(Collectors.groupingBy(UserEquipmentRelationDO::getUserCode));

            for (Map.Entry<String, List<UserEquipmentRelationDO>> entry:collect.entrySet()){

                List<String> equipmentIds = new ArrayList<>();
                String key = entry.getKey();
                List<UserEquipmentRelationDO> userEquipmentRelationDOList = entry.getValue();

                userEquipmentRelationDOList.stream().forEach(a -> equipmentIds.add(a.getEquipmentId()));

                UserEquipmentQueryVO userEquipmentQueryVO = new UserEquipmentQueryVO();

                userEquipmentQueryVO.setEquipmentIds(equipmentIds);
                userEquipmentQueryVO.setUserCode(key);
                list.add(userEquipmentQueryVO);
            }
        }
        return list;
    }

    @Override
    public void insertUserEquipmentRelation(UserEquipmentQueryVO userEquipmentQueryVO) {

        if (!userEquipmentQueryVO.getEquipmentIds().isEmpty()){
            List<UserEquipmentRelationDO> list = new ArrayList<>();

            for (String equipmentId : userEquipmentQueryVO.getEquipmentIds()) {

                UserEquipmentRelationDO userEquipmentRelationDO = new UserEquipmentRelationDO();
                userEquipmentRelationDO.setUserCode(userEquipmentQueryVO.getUserCode());
                userEquipmentRelationDO.setEquipmentId(equipmentId);
                list.add(userEquipmentRelationDO);
            }
            userEquipmentRelationDAO.insertBatch(list);
        }
    }

    @Override
    public void deleteUserEquipmentRelation(UserEquipmentQueryVO userEquipmentQueryVO) {

        if (!userEquipmentQueryVO.getEquipmentIds().isEmpty()){
            QueryWrapper<UserEquipmentRelationDO> queryWrapper = new QueryWrapper<>();

            queryWrapper.eq("user_code",userEquipmentQueryVO.getUserCode())
                    .in("equipment_id",userEquipmentQueryVO.getEquipmentIds());

            userEquipmentRelationDAO.delete(queryWrapper);
        }
    }

    @Override
    public List<UserEquipmentQueryVO> queryUserEquipmentRelationByEquipmentIds(List<String> equipmentIdslist) {

        List<UserEquipmentQueryVO> list = new ArrayList<>();

        if (!equipmentIdslist.isEmpty()){

            QueryWrapper<UserEquipmentRelationDO> queryWrapper = new QueryWrapper<>();

            queryWrapper.in("equipment_id",equipmentIdslist);

            List<UserEquipmentRelationDO> userEquipmentRelationDOS = userEquipmentRelationDAO.selectList(queryWrapper);

            Map<String, List<UserEquipmentRelationDO>> collect = userEquipmentRelationDOS.stream().collect(Collectors.groupingBy(UserEquipmentRelationDO::getUserCode));

            for (Map.Entry<String, List<UserEquipmentRelationDO>> entry:collect.entrySet()){

                List<String> equipmentIds = new ArrayList<>();
                String key = entry.getKey();
                List<UserEquipmentRelationDO> userEquipmentRelationDOList = entry.getValue();

                userEquipmentRelationDOList.stream().forEach(a -> equipmentIds.add(a.getEquipmentId()));

                UserEquipmentQueryVO userEquipmentQueryVO = new UserEquipmentQueryVO();

                userEquipmentQueryVO.setEquipmentIds(equipmentIds);
                userEquipmentQueryVO.setUserCode(key);
                list.add(userEquipmentQueryVO);
            }
        }
        return list;
    }

    @Override
    public List<EquipmentInfoVO> queryEquipmentsByUser(UserEquipmentQueryVO userEquipmentQueryVO) {
        List<UserEquipmentQueryVO> userEquipmentQueryVOS = queryUserEquipmentRelationByCodes(Collections.singletonList(userEquipmentQueryVO.getUserCode()));

        if (!userEquipmentQueryVOS.isEmpty()){

            UserEquipmentQueryVO queriedUserEquipment = userEquipmentQueryVOS.get(0);
            List<Long> equipmentIdList = queriedUserEquipment.getEquipmentIds().stream().map(Long::parseLong).collect(Collectors.toList());
            EquipmentInfoQueryPageParamDTO equipmentInfoQueryPageParamDTO = new EquipmentInfoQueryPageParamDTO();
            equipmentInfoQueryPageParamDTO.setIdList(equipmentIdList);
            equipmentInfoQueryPageParamDTO.setStatus(0);
            List<EquipmentInfoDTO> equipmentInfoDTOS = equipmentInfoService.listByParam(equipmentInfoQueryPageParamDTO);
            return BeanUtils.copyToList(equipmentInfoDTOS, EquipmentInfoVO.class);
        }
        return null;
    }

}
